package com.wx.xg.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wx.xg.mapper.WeekMapper;
import com.wx.xg.pojo.User;
import com.wx.xg.pojo.Week;
import com.wx.xg.service.UserService;
import com.wx.xg.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 54
 * @since 2021-05-29
 * <p>
 * 用户周计划表
 */
@RestController
@Api(tags = "用户周计划表")
@RequestMapping("/week")
public class WeekController {

    @Autowired
    private WeekMapper weekMapper;

    @Autowired
    private UserService userService;

    @RequestMapping(path = "/login/{uId}", method = RequestMethod.GET)
    @ApiOperation(value = "根据id显示用户周计划")
    public R weekById(@PathVariable("uId") Integer uId) {

        String uIdNull = "用户不存在";
        if (uId == null) {
            return R.error().data("userName", uIdNull);
        }
        QueryWrapper<Week> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("u_id", uId);
        List<Week> weeks = weekMapper.selectList(queryWrapper);
        return R.ok().data("weeks", weeks);
    }


    @RequestMapping(path = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "用户上传周计划")
    public R update(@RequestBody Week week) {
        String getUIdNull = "非法上传";
        if (week.getUId() == null) {
            return R.error().data("getUIdNull", getUIdNull);
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("u_id", week.getUId());
        int count = userService.count(queryWrapper);
        if (count > 0) {
            weekMapper.insert(week);
            return R.ok();
        }
        return R.error();
    }


}
